<?php
namespace app\common\model;

use think\Model;

class UserTotalModel extends Model{
	//表名
	protected $table = 'ly_user_total';

    public function addAmount($uid,$amount,$type,$remark){
        $total = model('UserTotal')->where('uid',$uid)->find();
        if ($amount<0&&floatval($total['balance'])<abs($amount)){
            return 0;
        }
        db('UserTotal')->where('uid',$uid)->update(['balance'=>$total['balance']+$amount]);
        db('UserTotal')->where('uid',$uid)->update(['total_balance'=>$total['total_balance']+$amount]);

        //资金流水记录
        $userInfo=db('users')->where(['id'=>$uid])->find();
        $userTotal=db('user_total')->where(['uid'=>$uid])->find();
        $financial_data_p['uid'] 					= $uid;
        $financial_data_p['username'] 				= $userInfo['username'];
        $financial_data_p['order_number'] 			= 'S'.date('YmdHis').rand(0,9).rand(0,9).rand(0,9).rand(0,9);
        $financial_data_p['trade_number'] 			= 'L'.trading_number();;
        $financial_data_p['trade_type'] 			= $type;
        $financial_data_p['trade_before_balance']	= $total['balance'];
        $financial_data_p['trade_amount'] 			= $amount;
        $financial_data_p['account_balance'] 		= $userTotal['balance'];
        $financial_data_p['remarks'] 				=  $remark;
        $financial_data_p['types'] 					= 1;
        model('TradeDetails')->tradeDetails($financial_data_p);

        return 1;
    }

}
